import { createRouter, createWebHashHistory } from 'vue-router'
import NotFound from '@/views/not-found/index.vue'
import Layout from '@/layout/index.vue'
import { useSimulationStore } from '@/stores/modules/simulation'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      component: Layout,
      redirect: '/originOil',
      children: [
        {
          path: '/originOil',
          name: 'originOil',
          redirect: '/originOil/industrialProcesses',
          meta: {
            title: 'originOil',
            icon: 'originOil'
          },
          children: [
            {
              path: '/originOil/industrialProcesses',
              // name: 'industrialProcesses',
              redirect: '/originOil/industrialProcesses/pipelineOverview',
              meta: {
                title: 'industrialProcesses',
                icon: 'industrialProcesses'
              },
              children: [
                {
                  path: '/originOil/industrialProcesses/pipelineOverview',
                  component: () => import('@/views/pipeline-overview/index.vue'),
                  meta: {
                    title: 'pipelineOverview',
                    icon: 'pipelineOverview'
                  }
                },
                {
                  path: '/originOil/industrialProcesses/graph',
                  component: () => import('@/views/graph/index.vue'),
                  meta: {
                    title: 'graph',
                    icon: 'graph'
                  }
                }
              ]
            },
            {
              path: '/originOil/trendForecasting',
              redirect: '/originOil/trendForecasting/parameterCalibration',
              meta: {
                title: 'trendForecasting',
                icon: 'trendForecasting'
              },
              children: [
                {
                  path: '/originOil/trendForecasting/parameterCalibration',
                  component: () => import('@/views/parameter-calibration/index.vue'),
                  meta: {
                    title: 'parameterCalibration',
                    icon: 'parameterCalibration'
                  }
                },
                {
                  path: '/originOil/trendForecasting/operatingConditions',
                  component: () => import('@/views/operating-conditions/index.vue'),
                  meta: {
                    title: 'operatingConditions',
                    icon: 'operatingConditions'
                  }
                },
                {
                  path: '/originOil/spatiotemporalPrediction/graph',
                  component: () => import('@/views/spatiotemporal-prediction/index.vue'),
                  meta: {
                    title: 'spatiotemporalPrediction',
                    icon: 'spatiotemporalPrediction'
                  }
                }
              ]
            },
            {
              path: '/originOil/decisionMaking',
              redirect: '/originOil/decisionMaking/appointmentWarning',
              meta: {
                title: 'decisionMaking',
                icon: 'decisionMaking'
              },
              children: [
                {
                  path: '/originOil/decisionMaking/appointmentWarning',
                  component: () => import('@/views/appointment-warning/index.vue'),
                  meta: {
                    title: 'appointmentWarning',
                    icon: 'appointmentWarning'
                  }
                },
                {
                  path: '/originOil/decisionMaking/energyOptimization',
                  component: () => import('@/views/energy-optimization/index.vue'),
                  meta: {
                    title: 'energyOptimization',
                    icon: 'energyOptimization'
                  }
                }
              ]
            }
          ]
        },
        {
          path: '/finishedOil',
          name: 'finishedOil',
          component: () => import('@/views/finished-oil/index.vue'),
          meta: {
            title: 'finishedOil',
            icon: 'finishedOil'
          }
        },
        {
          path: '/gasPipeline',
          name: 'gasPipeline',
          component: () => import('@/views/gas-pipeline/index.vue'),
          meta: {
            title: 'gasPipeline',
            icon: 'gasPipeline'
          }
        }
      ]
    },
    {
      path: '/:pathMatch(.*)',
      name: 'notFound',
      component: NotFound
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    // 始终滚动到顶部
    return { top: 0 }
  }
})
router.beforeEach(async (to, from) => {
  console.log("🐯 ~ router.beforeEach ~ to:", to)
  const simulationStore = useSimulationStore()
  const isRouteToPrediction = computed(() => simulationStore.isRouteToPrediction)
  
  // if (to.path === '/originOil/spatiotemporalPrediction/graph') {
  //   if (!isRouteToPrediction.value) {
  //     ElMessage.error('请先进行预测工况设置')
  //     return '/originOil/trendForecasting/operatingConditions'
  //   }
  //   // 如果条件满足，不需要返回任何值，让路由继续正常进行
  // }
})

export default router
